/*
 * EventGenerator.java
 * 
 * author: The MomberBan Team
 *    date: 19.09.2008
 * 
 * 
 */
package at.momberban.game.me;

import java.util.Vector;

/** 
 * splits the network from the game logic. a network manager doesn't know how a
 * event is going to be created and further processed in the game. it also
 * allows one object to observe if new events are available.
 * @author patchinsky
 */
public class EventGenerator {
    private Vector observer;
    
    /**
     * constructor
     * 
     */
    public EventGenerator() {
        this.observer = new Vector();
    }

    /**
     * register an observer
     * 
     * @param who
     *            to set
     */
    public void register(EventListener who) {
        this.observer.addElement(who);
    }
    
    /**
     * unregister an observer
     * 
     * @param who
     */
    public void unregister(EventListener who) {
        this.observer.removeElement(who);
    }

    /**
     * enqueue the event in the queue
     * 
     * @param data
     */
    public void write(String data) {
        Logger.log("nw-incoming: " + data);
        Event e = Event.decode(data);

        if (e != null) {
            Event.getEventQueue().add(e);
            for (int i = 0; i < this.observer.size(); i++) {
                Logger.log("notify listener: " + i);
                ((EventListener) this.observer.elementAt(i)).wakeUp();
            }
        } else {
            // invalid event!
            Logger.log("invalid event. skipped");
        }
    }

}
